package com.luoye.gifmaker;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;
import android.os.*;
import com.luoye.gifmaker.*;
import com.luoye.gifmaker.core.*;
import com.luoye.gifmaker.other.*;
import android.util.*;
import android.app.*;
import android.view.*;
import java.lang.reflect.*;

public class HCActivity extends Activity implements Runnable,OnClickListener,OnCreateContextMenuListener
{
	SharedPreferences sp;
	GridView list;
	Bitmap bitmapItem=null;
	ProgressDialog pa;
	Button bn_add,bn_clear,bn_hc;
	AnimatedGIFEncoder ag;
	List<Map<String, Object>> items ;
	Map<String, Object> item;
	SimpleAdapter adapter;
	SimpleDateFormat formatter;
	String preferencePath;
	String nowStr;
	AlertDialog.Builder adlog;
	final String THIS_ACTION= "hcAction";
	//final String ywfs="http://ywfs.blog.163.com/";
    /** Called when the activity is first created. */
	class MenuId{
		public final static int ID_SETDELAY_ITEM=100;
		public final static int ID_REMOVE_ITEM=101;
	};
	
    @Override
    public void onCreate (Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hc);
		//overridePendingTransition(R.anim.out_to_right, R.anim.in_from_left);
		bn_add = (Button)findViewById(R.id.bn_add);
		bn_clear = (Button)findViewById(R.id.bn_clear);
		bn_hc = (Button)findViewById(R.id.bn_hc);
		list = (GridView)findViewById(R.id.list);

		list.setOnTouchListener(null);
		bn_add.setOnClickListener(this);
		bn_clear.setOnClickListener(this);
		bn_hc.setOnClickListener(this);
		IntentFilter intentFilter=new IntentFilter();
		intentFilter.addAction(THIS_ACTION);
		registerReceiver(br, intentFilter);
		//FileList.curpath = "/mnt/sdcard";
		FileList.curpath = Environment.getExternalStorageDirectory().getAbsolutePath();
		FileList.broadcastAction = THIS_ACTION;
		initAdapter();
		formatter = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss"); 
		//广告初始化
		//BaiduManager.init(this);

		sp = PreferenceManager.getDefaultSharedPreferences(this);
		list.setOnCreateContextMenuListener(this);
//		showToast(getActionBar().getHeight()+"");
		
    }
	@Override
	public void onCreateContextMenu (ContextMenu p1, View p2, ContextMenu.ContextMenuInfo p3)
	{
		// TODO: Implement this method
		//p1.setHeaderTitle("选项");
		//第二个参数是id
		p1.add(0, MenuId.ID_SETDELAY_ITEM, 0, "设置持续时间");
		p1.add(0, MenuId.ID_REMOVE_ITEM, 0, "移除该图片");

	}

	//点击Context菜单的项时发生
	@Override
	public boolean onContextItemSelected (MenuItem item)
	{
		AdapterContextMenuInfo menuInfo =
			(AdapterContextMenuInfo) item.getMenuInfo();
		// TODO: Implement this method
		if (item.getItemId() == MenuId.ID_SETDELAY_ITEM)
		{
			//修改持续时间
			showEditTextDialog(HCActivity.this, "设置该图持续的时间(秒)", menuInfo.position);
		}
		else if (item.getItemId() == MenuId.ID_REMOVE_ITEM)
		{
			//移除项
			items.remove(menuInfo.position);
			adapter.notifyDataSetChanged();
			showToast("移除成功");
		}
		return super.onOptionsItemSelected(item);
	} 

	@Override
	public void run ()
	{
		// TODO: Implement this method
		ag = new AnimatedGIFEncoder();
		//如果不使用第一帧的大小
		if (!sp.getBoolean("cb_use_first_size", true))
			ag.setSize(
				Integer.parseInt(sp.getString("edit_pic_width", "480")),
				Integer.parseInt(sp.getString("edit_pic_height", "800"))
			);

		Date curDate = new Date(System.currentTimeMillis());//获取当前时间 
		nowStr = formatter.format(curDate);//转换时间格式

		preferencePath = sp.getString("edit_output_path1", "/mnt/sdcard/GIFMaker/gif");

		ag.start(preferencePath + "/" + nowStr + ".gif");
		ag.setRepeat(0);
		for (int i=0;i < items.size();i++)
		{
			ag.setDelay((int)(Float.parseFloat(items.get(i).get("delay").toString()) * 1000));
			try
			{
				ag.addFrame(BitmapFactory.decodeFile(items.get(i).get("filePath").toString()));
			}
			//要生成的图片太大，内存不足
			catch (OutOfMemoryError oome)
			{
				handler.sendEmptyMessage(1);
				return;
			}

		}
		//记得收尾
		ag.finish();
		handler.sendEmptyMessage(0);
	}

	//载入列表，可以节省时间
	Runnable loadFileList=new Runnable()
	{

		@Override
		public void run ()
		{
			// TODO: Implement this method
			FileList.filter = ".*.jpg|.*.png|.*.bmp|.*.gif$";
			//FileList.curpath=;
			FileList.getListFromPath(FileList.curpath);
		}


	};

	Handler handler=new Handler(){

		@Override
		public void handleMessage (Message msg)
		{
			// TODO: Implement this method
			if (msg.what == 0)
			{
				pa.dismiss();
				adlog = new AlertDialog.Builder(HCActivity.this);
				adlog.setTitle("合成完成")
					.setMessage("合成已完成" + "\n保存路径：" + sp.getString("edit_output_path1", "/mnt/sdcard/GIFMaker/gif") + "/" + nowStr + ".gif")
					.setNegativeButton("确定", null)
					.setPositiveButton("查看图片", new DialogInterface.OnClickListener(){

						@Override
						public void onClick (DialogInterface p1, int p2)
						{
							// TODO: Implement this method
							Intent intent=new Intent(HCActivity.this, GifViewActivity.class);
							intent.putExtra("imagePath", sp.getString("edit_output_path1", "/mnt/sdcard/GIFMaker/gif") + "/" + nowStr + ".gif");
							startActivity(intent);
						}


					})
					.show();
				showToast("合成完成");

			}
			else if (msg.what == 1)
			{
				pa.dismiss();
				adlog = new AlertDialog.Builder(HCActivity.this);
				adlog.setTitle("合成未完成")
					.setMessage("要生成的图片太大，无法生成")
					.setNegativeButton("确定", null)
					//.setIcon(android.R.drawable.ic_menu_help)
					.show();
				showToast("图片生成失败");
			}
		}

	};
	//按钮事件*********
	@Override
	public void onClick (View p1)
	{
		// TODO: Implement this method
		if (p1.getId() == R.id.bn_add)
		{

			Intent intent=new Intent();
			intent.setClass(HCActivity.this, FileList.class);
			startActivity(intent);
			//overridePendingTransition(R.anim.out_to_bottom, R.anim.in_from_bottom);
		}
		else if (p1.getId() == R.id.bn_clear)
		{
			if (items.size() > 0)
			{

				adlog = new AlertDialog.Builder(this);
				adlog.setTitle("提示")
					.setMessage("确定清空列表吗？")
					.setPositiveButton("确定", new DialogInterface.OnClickListener(){
						@Override
						public void onClick (DialogInterface p1, int p2)
						{
							// TODO: Implement this method
							items.clear();
							adapter.notifyDataSetChanged();
						}
					})
					.setNegativeButton("取消", null)
					.show();
			}
		}
		else if (p1.getId() == R.id.bn_hc)
		{
			if (items.size() > 0)
			{
				pa = ProgressDialog.show(HCActivity.this, null, "正在合成……");
				//让它不可返回
				pa.setCancelable(false);
				new Thread(this).start();
			}
			else
				showToast("请先添加图片");

		}
	}

	@Override
	public Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter)
	{
		// TODO: Implement this method
		return super.registerReceiver(receiver, filter);
	}

	//实例化一个广播接收器
	BroadcastReceiver br=new BroadcastReceiver()
	{
		@Override
		public void onReceive (Context p1, Intent p2)
		{
			// TODO: Implement this method
			if (p2.getAction().equals(THIS_ACTION))
			{
				bitmapItem = IO.getImageFromPath(FileList.selectedFilePath);

				item = new HashMap<String, Object>();    
				item.put("imageItem", bitmapItem); 
				item.put("filePath", FileList.selectedFilePath);
				item.put("fileName", new File(FileList.selectedFilePath).getName());
				item.put("shortName", FileList.getShortName(new File(FileList.selectedFilePath).getName(), 4, 2));
				item.put("delay", sp.getString("edit_dely", "0.5"));

				items.add(item);  

				adapter.notifyDataSetChanged();

				list.setSelection(items.size() - 1);
			}
		}
	};
	//设置时间的对话框
	public void showEditTextDialog (Context context, String title, final int itemPosition)
	{
		final EditText edit=new EditText(context);
		//设置只能输入小数
		edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		edit.setText(items.get(itemPosition).get("delay") + "");
		new AlertDialog.Builder(context)
			.setTitle(title)
			.setView(edit)
			.setPositiveButton("确定", 
			new DialogInterface.OnClickListener(){
				@Override
				public void onClick (DialogInterface p1, int p2)
				{
					// TODO: Implement this method
					items.get(itemPosition).put("delay", edit.getText().toString());
					adapter.notifyDataSetChanged();
				}
			})
			.setNegativeButton("取消", null).show();
	}

	public void showToast (CharSequence text)
	{
		Toast.makeText(HCActivity.this, text, 2000).show();
	}

	//初始化adapter
	public void initAdapter ()
	{
		items = new ArrayList<Map<String,Object>>(); 
		//实例化一个适配器  
		adapter = new SimpleAdapter(HCActivity.this, items,
									R.layout.hc_list_item,
									new String[]{"imageItem", "shortName","delay","filePath"},
									new int[]{R.id.image_item, R.id.hc_tv_filename,R.id.tv_delay}
									);  

		list.setAdapter(adapter);  
		adapter.setViewBinder(new ViewBinder(){

				@Override
				public boolean setViewValue (View p1, Object p2, String p3)
				{
					if (p1 instanceof ImageView && p2 instanceof Bitmap)
					{
						ImageView iv = (ImageView) p1;
						iv.setImageBitmap((Bitmap) p2);
						return true;
					}
					else
						return false;

				}
			});
	}


	long cur=0;
	@Override
	public void onBackPressed ()
	{
		if (System.currentTimeMillis() - cur > 2000)
		{
			cur = System.currentTimeMillis();
			Toast.makeText(HCActivity.this, "再按一次返回键退出", 0).show();
		}
		else
		{
			finish();
			System.exit(0);
		}
	}


	@Override
	protected void onResume ()
	{
		// TODO: Implement this method
		//当Activity被重新激活时
		//要重新给动作和筛选条件重新赋值
		//不让两个Activity的ListView和选择文件时乱
		FileList.broadcastAction = THIS_ACTION;
		FileList.filter = ".*.jpg|.*.png|.*.bmp|.*.gif$";
		//清空缓存数组
		FileList.dirs_cache = null;
		FileList.files_cache = null;
		//开启线程载入列表
		new Thread(loadFileList).start();
		//FileList.dirs_name=null;
		//FileList.files_name=null;
		//showToast(THIS_ACTION);
		super.onResume();
	}

}
